|
In the area of mathematical logic and computer science known as type theory, a unit type is a type that allows only one value (and thus can hold no information). The carrier (underlying set) associated with a unit type can be any singleton set. There is an isomorphism between any two such sets, so it is customary to talk about ''the'' unit type and ignore the details of its value. One may also regard the unit type as the type of 0-tuples, i.e. the product of no types. The unit type is the terminal object in the category of types and typed functions. It should not be confused with the ''zero'' or bottom type, which allows ''no'' values and is the initial object in this category. The unit type is implemented in most functional programming languages. The void type that is used in some imperative programming languages serves some of its functions, but because its carrier set is empty, it has some limitations (as detailed below). ==In programming languages== Several computer programming languages provide a unit type to specify the result type of a function with the sole purpose of causing a side effect, and the argument type of a function that does not require arguments. *In Haskell and Rust, the unit type is called () and its only value is also () , reflecting the 0-tuple interpretation. *In ML (including OCaml and Standard ML), the type is called unit but the value is written as () . *In Scala, the unit type is called Unit and its only value is written as () .*In Common Lisp the type named NULL is a unit type which has one value, namely the symbol NIL. This should not be confused with the NIL ''type'', which is the bottom type. * In Python, there is a type called NoneType which allows the single value of None .* In C++11, there is a type called nullptr_t (null pointer type) that can only store the value nullptr .* In Swift, the unit type is called Void or () and its only value is also () , reflecting the 0-tuple interpretation.* In Go, the unit type is written struct .The unit type is useful even in functions without side effects if the programming language supports algebraic data types. Any nullary data constructor is effectively isomorphic with the unit type. One can solve the semipredicate problem (the problem of distinguishing between a "normal" return value of a function and an "error") elegantly in such a language, by encoding the "error" as the unit type. In Haskell, the polymorphic type Maybe is predefined for this purpose. Here Nothing is isomorphic to the unit type:The type Maybe is called the option type in type theory.抄文引用元・出典: フリー百科事典『 ウィキペディア(Wikipedia)』 ■ウィキペディアで「Unit type」の詳細全文を読む スポンサード リンク
|